package zhr.chapter13;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        new TCPserver().listen();
    }
    static class TCPserver{
        private static  final int PORT =7788;
        public void listen() throws Exception{
            ServerSocket serverSocket=new ServerSocket(PORT);
            Socket client=serverSocket.accept();
            //发
            OutputStream os =client.getOutputStream();
            //收
            InputStream osin=client.getInputStream();
            System.out.println("开始与客户端交互数据");

            os.write(("服务器欢迎您!").getBytes());

            byte[] buf=new byte[1024];
            int len=osin.read(buf);
            System.out.println(new String(buf,0,len));

            Thread.sleep(5000);
            System.out.println("结束与客户端的数据交互");
            os.close();
            client.close();
        }
    }
}
